﻿using Microsoft.Extensions.Logging;
using Simple.Framework;
using Simple.Framework.Ioc;
using Simple.Tools;
using SimpleNet.Net;
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

/*********************************************************
 * 命名空间 SimpleNet.Handle
 * 接口名称 StringMessageHandle
 * 开发人员：ADMIN
 * 创建时间：2023/12/29 11:17:44
 * 描述说明：
 * 更改历史：
 * 
 * *******************************************************/
namespace SimpleNet.Handle
{
	public class StringMessageHandle :MessageHandle,IMessageHandle
	{
		private SystemConfiguration sysCfg;

		
		public StringMessageHandle(IContainer container) : base(container)
		{
			sysCfg = container.GetService<SystemConfiguration>();
		}

		public override void AnalysisHandle(BlockingCollection<IDataPack> qdataPacks, byte[] data, int length)
		{
			base.AnalysisHandle(qdataPacks, data, length);

		}

		public override void Handle(IContext context)
		{
		
			var handle = (BaseStringHandle)container.GetService(sysCfg.StringMessageType);
			if (sysCfg.StringMessageType==null)
			{
				ErrorInfo();
			}
			if (handle!=null)
			{
				handle.ReceiveMessage(Encoding.Default.GetString(context.CurrentPack.ReceiveBuffer),context);
			}
			else
			{
				ErrorInfo();
			}
		}

		public override void Handled(IContext context)
		{
		}

		public override void SetDataPack(IDataPack dataPack)
		{
			dataPack.Complete = true;
		}

		private void ErrorInfo()
		{
			logger.LogError($"1.请继承BaseStringHandle!");
			logger.LogError($"2.否则无法解析消息!");
		}
	}
}
